/*
This file creates a variable to label "defaulters", as that term is defined
in the paper, and related concepts.
*/


**** Get the month 4 years before the survey
* We want to keep only respondents that were current at least once within 4
* years of the survey, to avoid people that have been chronically in default
* for many years (probably since the Great Recession) and just never
* apparently went through foreclosure.
* Although different respondents filled out the survey at different times,
* and we don't know precisely when they filled it out, we can get a good guess.
* Note according to Saty, these are the dates the surveys were in the field
* 2016: 8/16 to 11/18, 2017: 7/25 to 10/31, 2018: 6/26 to 10/2
* See Table C7 in the NMDB documentation for month codes.
gen month_4yrs_before_survey = 176 if survey_year==2016 //Month 176 is August 2012
replace month_4yrs_before_survey = 187 if survey_year == 2017 //Month 187 is July 2013
replace month_4yrs_before_survey = 198 if survey_year == 2018 //Month 198 is June 2014

**** Also get the month 2 years before the survey
gen month_2yrs_before_survey = month_4yrs_before_survey + 24

**** Also get the month the survey ended
gen month_survey_ends = 227 if survey_year==2016 //Month 227 is November 2016
replace month_survey_ends  = 238 if survey_year==2017 //Month 238 is October 2017
replace month_survey_ends  = 249 if survey_year==2018 //Month 249 is September 2018

**** Using the dates we have, define important concepts
gen defaulter = 0
gen last_current_month_b4_default = .
gen last_current_month_b4_delinq = .
gen current_by_survey  = 0
gen dpd_status = 0
forvalues mm = 277(-1)42 {
	
	* Label max days past due, with 1 being "<60 DPD", 2 being "60-89 DPD", and 3 being "90+ DPD"
	replace dpd_status = 1 if dpd_status==0 & gridm`mm' == "1" & `mm' >= month_2yrs_before_survey & `mm' <= month_survey_ends 
	replace dpd_status = 2 if dpd_status<=1 & gridm`mm' == "2" & `mm' >= month_2yrs_before_survey & `mm' <= month_survey_ends 
	replace dpd_status = 3 if dpd_status<=2 & inlist(gridm`mm',"3","4","5","6","7","8","9") & `mm' >= month_2yrs_before_survey & `mm' <= month_survey_ends 

	
	* Define a defaulter as someone who was 90+ days delinquent within 2 
	* (not 4!) years of the survey.  The reason to use 2, and not 4,
	* is to approximate the "delinquent" subsample of the ASMB.  
	replace defaulter = 1 if inlist(gridm`mm',"3","4","5","6","7","8","9") & `mm' >= month_2yrs_before_survey & `mm' <= month_survey_ends 
	
	* Get the last month *before* a default that a defaulter was current.
	* For this, go back to 4 years before the survey, but no further.
	replace last_current_month_b4_default = `mm' if gridm`mm'=="C" & defaulter==1 & mi(last_current_month_b4_default) & `mm' >= month_4yrs_before_survey & `mm' <= month_survey_ends 
	replace last_current_month_b4_delinq = `mm' if gridm`mm'=="C" & inlist(dpd_status,1,2,3) & mi(last_current_month_b4_delinq) & `mm' >= month_4yrs_before_survey & `mm' <= month_survey_ends 

	
}

* Label defaulters who are current by the time the survey is in the field
* Note this will not count people who prepaid their mortgage or sold their home
replace current_by_survey = 1 if gridm227=="C" & survey_year==2016
replace current_by_survey = 1 if gridm238=="C" & survey_year==2017
replace current_by_survey = 1 if gridm249=="C" & survey_year==2018



